﻿Imports System.IO

Public Class Config

    Public Shared LoginServerIP As String
    Public Shared LoginServerPort As Integer

    Public Shared ClusterServerIP As String
    Public Shared ClusterServerPort As Integer
    Public Shared ClusterServerName As String

    Public Shared WorldServerIP As String
    Public Shared WorldServerPort As Integer

    Public Shared ISCIP As String
    Public Shared ISCLoginServerPort As Integer
    Public Shared ISCClusterServerPort As Integer
    Public Shared ISCWorldServerPort As Integer

    Public Shared MySQLServer As String
    Public Shared MySQLDatabase As String
    Public Shared MySQLUsername As String
    Public Shared MySQLPassword As String

    Public Shared Sub startConfig()
        If IO.File.Exists("servers.ini") Then
            doConfig()
        Else
            makeConfig()
        End If
    End Sub

    Public Shared Sub doConfig()
        Dim stream As New FileStream("servers.ini", FileMode.OpenOrCreate, _
                                     Security.AccessControl.FileSystemRights.Read, FileShare.Read, _
                                     1000, FileOptions.None)
        Dim reader As New StreamReader(stream)
        Dim file = reader.ReadToEnd()
        Dim strArr = file.Split("#")

        Dim LoginServerArr = strArr(1).Split(":")
        Dim ClusterServerArr = strArr(2).Split(":")
        Dim WorldServerArr = strArr(3).Split(":")
        Dim ISCArr = strArr(4).Split(":")
        Dim MySQLArr = strArr(5).Split(":")

        'LoginServer
        LoginServerIP = LoginServerArr(1).Split(";")(0)
        LoginServerPort = LoginServerArr(2).Split(";")(0)

        'ClusterServer
        ClusterServerIP = ClusterServerArr(1).Split(";")(0)
        ClusterServerPort = ClusterServerArr(2).Split(";")(0)
        ClusterServerName = ClusterServerArr(3).Split(";")(0)

        'WorldServer
        WorldServerIP = WorldServerArr(1).Split(";")(0)
        WorldServerPort = WorldServerArr(2).Split(";")(0)

        'ISC
        ISCIP = ISCArr(1).Split(";")(0)
        ISCLoginServerPort = ISCArr(2).Split(";")(0)
        ISCClusterServerPort = ISCArr(3).Split(";")(0)
        ISCWorldServerPort = ISCArr(4).Split(";")(0)

        'MYSQL
        MySQLServer = MySQLArr(1).Split(";")(0)
        MySQLDatabase = MySQLArr(2).Split(";")(0)
        MySQLUsername = MySQLArr(3).Split(";")(0)
        MySQLPassword = MySQLArr(4).Split(";")(0)

        log.log(LogType.MSG_INFO, "Config file loaded")
    End Sub

    Public Shared Sub makeConfig()
        Dim stream As New FileStream("servers.ini", FileMode.Create, _
                                     Security.AccessControl.FileSystemRights.Write, _
                                     FileShare.Write, 1000, FileOptions.None)
        Dim writer As New StreamWriter(stream)
        writer.WriteLine("[LoginServer]")
        writer.WriteLine("#")
        writer.WriteLine("IP :127.0.0.1;")
        writer.WriteLine("Port :23000;")
        writer.WriteLine()

        writer.WriteLine("[ClusterServer]")
        writer.WriteLine("#")
        writer.WriteLine("IP :127.0.0.1;")
        writer.WriteLine("Port :28000;")
        writer.WriteLine("Name :FlyFF;")
        writer.WriteLine()

        writer.WriteLine("[WorldServer]")
        writer.WriteLine("#")
        writer.WriteLine("IP :127.0.0.1;")
        writer.WriteLine("Port :15400;")
        writer.WriteLine()

        writer.WriteLine("[ISC]")
        writer.WriteLine("#")
        writer.WriteLine("IP :127.0.0.1;")
        writer.WriteLine("Login Server Port :22000;")
        writer.WriteLine("Cluster Server Port :27000;")
        writer.WriteLine("World Server Port :21000;")
        writer.WriteLine()

        writer.WriteLine("[MySQL]")
        writer.WriteLine("#")
        writer.WriteLine("Server :localhost;")
        writer.WriteLine("Database :myflyff;")
        writer.WriteLine("Username :root;")
        writer.WriteLine("Password :root;")
        writer.Close()
        doConfig()
    End Sub

End Class